package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.util.JsonUtil;
import com.coolbi.onsupai.core.biz.CustomerBiz;
import com.coolbi.onsupai.core.entity.Customer;
import com.coolbi.security.web.action.SessionAction;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 *@description:客户信息 Action
 *@author: cgn
 *@createdate :2012-7-1
 */
public class CustomerAction extends SessionAction {

	private static final long serialVersionUID = 1L;

	private Customer customer;
	private List<Customer> customerList;
	@Autowired
	private CustomerBiz customerBiz;
	private String searchType;
	private String keyWord;
	
	
	/**根据关键字查询*/
	public String findByKeyWord() throws Exception {
		try {
			pager.setRecordCount(customerBiz.countBykeyWord(searchType, keyWord));
			customerList = customerBiz.findByKeyWord(searchType, keyWord, (pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 添加客户信息
	 * @return
	 */
	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "customer.userName", message = "客户名称必填"), 
			@RequiredStringValidator(fieldName = "customer.realName", message = "真实姓名必填"), 
			@RequiredStringValidator(fieldName = "customer.address", message = "联系地址必填"), 
			@RequiredStringValidator(fieldName = "customer.phoneNumber", message = "联系电话必填")
	})
	public String save() {
		try {
			customerBiz.save(customer);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 分页查询客户信息
	 * @return
	 */
	public String listByPage() {
		pager.setRecordCount(customerBiz.count());
		customerList = customerBiz.listByPage((pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
		return SUCCESS;
	}
	/**
	 * 更新客户信息
	 * @return
	 */
	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "customer.userName", message = "客户名称必填"), 
			@RequiredStringValidator(fieldName = "customer.realName", message = "真实姓名必填"), 
			@RequiredStringValidator(fieldName = "customer.address", message = "联系地址必填"), 
			@RequiredStringValidator(fieldName = "customer.phoneNumber", message = "联系电话必填")
	})
	public String update(){
		try {
			customerBiz.update(customer);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	/**
	 * 删除客户信息
	 * @return
	 */
	public String delete(){
		try {
			customerBiz.delete(customer.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	/**
	 * 根据id查看顾客个人信息
	 * @return
	 */
	public String findById(){
		try {
			customer=customerBiz.get(customer.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 根据ID获取客户个人信息，返回json数据
	 * @return
	 */
	public String findByCustomerId() {
		try {
			customer = customerBiz.get(customer.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		String json = JsonUtil.toJsonByObject(customer);
		return ajax(json);
	}
	
	/**
	 * 检测手机号码是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkTelephone() {
		try {
			boolean flag = false;
			if (null == customer.getId()) {
				flag = customerBiz.checkTel(customer.getTelephone());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**
	 * 检测电子邮箱是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkEmail() {
		try {
			boolean flag = false;
			if (null == customer.getId()) {
				flag = customerBiz.checkEmail(customer.getEmail());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**
	 * 检测客户昵称是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkUserName() {
		try {
			boolean flag = false;
			if (null == customer.getId()) {
				flag = customerBiz.checkUserName(customer.getUserName());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**
	 * 检测QQ号码是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkQQNumber() {
		try {
			boolean flag = false;
			if (null == customer.getId()) {
				flag = customerBiz.checkQQNumber(customer.getQqNumber());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public List<Customer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customer> customerList) {
		this.customerList = customerList;
	}
	
	public void setCustomerBiz(CustomerBiz customerBiz) {
		this.customerBiz = customerBiz;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}
}
